local skel = fk.CreateSkill {
  name = "emo__junzhi",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__junzhi"] = "均质",
  [":emo__junzhi"] = "锁定技，每名角色的结束阶段，若你手牌花色不足四种，你获得缺少的花色各一张，然后若你手牌有重复花色，你须展示所有手牌并弃置一张多数花色的手牌。",

  ["#emo__junzhi-discard"] = "请弃置一张多数花色的手牌",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target.phase == Player.Finish then
      local list = {Card.Spade, Card.Heart, Card.Diamond, Card.Club}
      local hand = player:getCardIds("h")
      if #hand < 4 then return true end
      return table.find(list, function (suit)
        return table.every(hand, function (id) return Fk:getCardById(id).suit ~= suit end)
      end)
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local get = {}
    local list = {"spade", "heart", "diamond", "club"}
    local hand = player:getCardIds("h")
    for _, suit in ipairs(list) do
      if table.every(hand, function (id) return Fk:getCardById(id):getSuitString() ~= suit end) then
        local ids = room:getCardsFromPileByRule(".|.|"..suit, 1, "allPiles")
        if #ids > 0 then
          table.insert(get, ids[1])
        end
      end
    end
    if #get == 0 then return end
    room:obtainCard(player, get, true, fk.ReasonJustMove, player, skel.name)
    if player.dead or player:isKongcheng() then return end
    local suitMap = {}
    for _, id in ipairs(player:getCardIds("h")) do
      local suit = Fk:getCardById(id):getSuitString()
      suitMap[suit] = (suitMap[suit] or 0) + 1
    end
    local suits = {}
    for suit, num in pairs(suitMap) do
      if num > 1 then
        table.insert(suits, suit)
      end
    end
    if #suits == 0 then return end
    player:showCards(player:getCardIds("h"))
    room:askToDiscard(player, {
      min_num = 1, max_num = 1, include_equip = false, cancelable = false, skill_name = skel.name,
      pattern = ".|.|" .. table.concat(suits, ",") .. "|hand", prompt = "#emo__junzhi-discard",
    })
  end,
})

return skel
